#include "GameObject.h"
#include <time.h>
#include "MessageHandler.h"
#include"EntityManager.h"
#include "telegram.h"


//Singleton Instance
MessageHandler* MessageHandler::Instance()
{
  static MessageHandler instance;

  return &instance;
}
//Release the hounds! Er, messages!
void MessageHandler::Discharge(GameObject* pReceiver, const Telegram& msg)
{
	pReceiver->HandleMessage(msg);
}

//Give me your message, foo
void MessageHandler::SendMessage( double  delay,
									int    sender,
									int    receiver,
									int    msg,
									void*  ExtraInfo)
{
	if(sender>=0)
	{
		GameObject* pSender   = EntMgr->GetEntityFromID(sender);
	}

	GameObject* pReceiver = EntMgr->GetEntityFromID(receiver);
	Telegram telegram(0, sender, receiver, msg, ExtraInfo);

	if (delay <= 0.0f) 
	{
		Discharge(pReceiver, telegram);
	}
	else
	{
		double CurrentTime = clock();

		telegram.DispatchTime = CurrentTime + delay; 
	}
}
